package javango.forms.fields;

import java.util.Locale;
import java.util.Map;

import javango.forms.widgets.WidgetFactory;

import org.apache.commons.lang.StringUtils;

import com.google.inject.Inject;

public class BooleanField extends FieldBase {

	@Inject
	public BooleanField(WidgetFactory widgetFactory) {
		super(widgetFactory);
	}

	public Object clean(String value, Map<String, String> errors) {
		if (StringUtils.isEmpty(value)) {
			if (this.isRequired()) {
				errors.put(getName(), "Required");
				return null;
			} else {
				return null;
			}
		}
		
        String tmpStr = value.toLowerCase(Locale.getDefault());    	
        return ("false".equals(tmpStr) || "f".equals(tmpStr) || "no".equals(tmpStr) || "n".equals(tmpStr) || "off".equals(tmpStr)) ? false : true;
	}
}
